#include <my_header.h>
#include "../include/head.h"

//记录普通日志(客户端请求信息和连接时间这些)
int record_client_msg(int logfd,int fd_client,const char *rank,const char *msg){
    time_t origin_time;
    struct tm* timeinfo;
    //获取当前时间
    time(&origin_time);
    //转换时间
    timeinfo = localtime(&origin_time);

    //格式化
    char buf[1024];
    sprintf(buf,"%d年-%2d月-%2d日 %2d时:%2d分:%2d秒 [fd:%d] [%s] %s\n",
            timeinfo->tm_year+1900,
            timeinfo->tm_mon+1,
            timeinfo->tm_mday,
            timeinfo->tm_hour,
            timeinfo->tm_min,
            timeinfo->tm_sec,
            fd_client,
            rank,
            msg);
    //把信息存入日志中
    write(logfd,buf,strlen(buf));
    return 0;
}

//记录命令日志
int record_client_cmd(int logfd,int fd_client,const char* rank,Cmd_t pcmd){
    time_t oringin_time;
    struct tm* timeinfo;
    //获取
    time(&oringin_time);
    //转换
    timeinfo = localtime(&oringin_time);
    //格式化
    char buf[1024];
    sprintf(buf,"%d年-%2d月-%2d日 %2d时:%2d分:%2d秒 [fd:%d] [%s]\n",
            timeinfo->tm_year+1900,
            timeinfo->tm_mon+1,
            timeinfo->tm_mday,
            timeinfo->tm_hour,
            timeinfo->tm_min,
            timeinfo->tm_sec,
            fd_client,
            rank);
    //往格式化信息里再加入命令信息
    strcat(buf,HOME);
    for(int i = 0;i<pcmd.c_argc;++i){
        strcat(buf,"/");
        strcat(buf,pcmd._cmd[i]);
    }
    strcat(buf,"\n");
    //存入日志
    write(logfd,buf,strlen(buf));
    return 0;
}
